//
//  HomeViewModel.swift
//  SwiftSign
//
//  Created by continue on 2021/4/26.
//

import UIKit
import RxSwift
import RxCocoa

class HomeViewModel: ViewModel, ViewModelType {
    
    struct Input {
        let ready: Observable<Void>
    }
    
    struct Output {
//        let result: Driver<UserInitInfo>
        let loginSelected: Driver<LoginMenuViewModel>
    }
    
    func transform(input: Input) -> Output {
        let elements = PublishSubject<LoginMenuViewModel>()
        input.ready.flatMap { _ in
            return self.provider.userInit()
                .trackActivity(self.loading)
                .trackError(self.error)
        }.subscribe({ (event) in
            switch event {
            case .next(let data):
                print(data.toJSONString() as Any)
                let viewModel = LoginMenuViewModel(provider: self.provider, data)
                elements.onNext(viewModel)
            case .error(let error):
                LogError(error.localizedDescription)
            default: break
            }
        }).disposed(by: rx.disposeBag)
 
        
        return Output(loginSelected: elements.asDriverOnErrorJustComplete())
    }
}
